<?php
/**
*名称:ajax.php
*作用:ajax
*说明:
*版权:
*作者:Red	QQ:316765128
*时间:2011/8/25
**/
include_once './include/init.inc.php';
$user_id = getCookie('user_id');

if(empty($s)){
	exit();
}
$re = '';
if($s == 'qq_login'){	//登录
	$qq->redirect_to_login($Global['qq']['callback']);
	exit();
}elseif($s=='add_like'){
	$res = $db -> fetch_one("select goods_id from `info_goods_qlike` where `goods_id`='$id' and `user_id`='$user_id'");
	if(empty($res) and !empty($user_id)){
		$dateline = time();
		$db -> query("insert into `info_goods_qlike`(`goods_id`,`user_id`,`dateline`) values('$id','$user_id','$dateline')");
		exit('1');
	}else{
		exit('2');
	}
}elseif($s=='upload'){
	$smarty->assign('v',time());
	$smarty->display('upload.dwt');
	exit();
}elseif($s=='city'){
	if(empty($s1)){
		$default = '"==请选择省/直辖市==":{"key":" ","defaultvalue" : "","values":{"==城市/地区==":"==城市/地区=="}}';
	}else{
		$default = '"'.$s1.'":{"key":"'.$s1.'","defaultvalue" : ""}';
		$smarty->assign('s2',$s2);
	}
	$smarty->assign('default',$default);
	$smarty->display('city.dwt');
	exit();
}elseif($s=='dengji'){	//宠物登记
	empty($pet_id)?$pet_id='':'';
	empty($goods_id)?$goods_id='':'';
	$smarty->assign('pet_id',$pet_id);
	$smarty->assign('goods_id',$goods_id);
	$smarty->display('dengji.dwt');
	exit();
}elseif($s=='goods_sort'){	//后期优化，生成静态界面
	if(!empty($pet_id) and is_numeric($pet_id)){		//修改宠物资料
		$sql = "select * from `info_user_pet` where id='$pet_id'";
		$pets = $db -> fetch_one($sql);
		$smarty->assign('pets',$pets);
	}
	$sid2 = '';
	$sort = array('64'=>'大型犬','65'=>'中型犬','66'=>'小型犬','3'=>'猫咪','44'=>'水族');
	$sz_arr = array(115,116,117,118,119,120,121,122,123,124,125,126);
	if(empty($goods_id)){
		$goods_id = '';
		$list_goodsname = '"==请选择分类==":{"key":" ","defaultvalue" : "","values":{"==品种==":"==品种=="}}';
	}else{	//获取分类
		is_numeric($goods_id)?'':$goods_id='';
		$sql = "select `goods_name`,`cat_id` from `info_goods` where `goods_id`='$goods_id'";
		$res = $db->fetch_one($sql);
		if(!empty($res)){
			$sid2 = $goods_id;			//品种ID
			in_array($res['cat_id'],$sz_arr)?$sort_id='44':$sort_id=$res['cat_id'];	//获取分类ID
			$list_goodsname = '"'.$sort[$sort_id].'":{"key":"'.$sort[$sort_id].'","defaultvalue" : ""}';
		}else{
			$list_goodsname = '"==请选择分类==":{"key":" ","defaultvalue" : "","values":{"==品种==":"==品种=="}}';
		}
	}

	foreach($sort as $key=>$val){
		$sql = "select `goods_name`,`goods_id` from `info_goods` WHERE ";
		if($key=='44'){
			$sql .= "cat_id in (115,116,117,118,119,120,121,122,123,124,125,126)";
		}else{
			$sql .= 'cat_id='.$key;
		}
		$res = $db->fetch_all($sql);
		$list_goodsname .= ',';
		$list_goodsname .= '"'.$val.'":{';
		$list_goodsname .= '"key":"'.$val.'",';
		$list_goodsname .= '"defaultvalue" : "'.$sid2.'",';
		$list_goodsname .= '"values":{';
		$li = '';
		foreach($res as $key2=>$val2){
			$li .= ',"'.$val2['goods_name'].'":"'.$val2['goods_id'].'"';
		}
		if(!empty($li)){
			$li = substr($li,1);
		}
		$list_goodsname .= $li.'}}';
	}
	$smarty->assign('list_goodsname',$list_goodsname);
	$smarty->assign('goods_id',$goods_id);
	$smarty->display('goods_sort.dwt');
	exit();
}elseif($s == 'photo'){	//上传照片
	$upload = new UploadFile;
	$pimage = new pImageFile;
	$result = array();
	try{
		$imgName = '';
		if(is_uploaded_file($photo['tmp_name'])){
			empty($k)?$k='':'';
			if($k=='avatar'){
				$file_dir = ROOT.'upload/avatar/';
				$nwidth_height = 300;
				//$cType = 'resizeCrop';
				$cType = 'resize';
			}else{
				$file_dir = ROOT.'upload/pic/';
				$nwidth_height = 160;
				$cType = 'resize';
			}
			$imgName = $upload->upload($file_dir,$photo,10242880);
			$s_img = getImgName($imgName,'s');
			$pimage->pimage($cType, $imgName, $file_dir, $s_img, $nwidth_height, $nwidth_height);
			if($s_img !=''){
				$result["file"] = $s_img;
				$result["status"] = "1";
			}else{
				$result["status"] = "-2";
				//$result["error"] = "上传失败，请重试！";
			}
		}
	}catch(Exception $e){
		$result["status"]="-1";
		$result["file"]="";
	}
	echo json_encode($result);
	exit();
}elseif($s=='add_pet'){		//添加宠物
	$name = contentReplace($name);
	$province = contentReplace($province);
	$city = contentReplace($city);
	$pic = contentReplace($pic);
	$sex = contentReplace($sex);
	//$sort = contentReplace($sort);
	$kind = contentReplace($kind);
	$goods_id = contentReplace($goods_id);
	$user_id = contentReplace($user_id);

	$dateline = time();
	if(!empty($user_id)){
		$goods = $db -> fetch_one("select goods_name from info_goods where goods_id = '$goods_id'");
		$db -> query("insert into `info_user_pet`(`user_id`,`goods_id`,`goods_name`,`name`,`province`,`city`,`pic`,`sex`,`kind`,`dateline`) values('$user_id','$goods_id','".$goods['goods_name']."','$name','$province','$city','$pic','$sex','$kind','$dateline')");
		exit('1');
	}else{
		exit('2');
	}
}elseif($s=='comm_num'){
	$num = $db -> fetch_one("select num from commend");
	$num = $num['num']*1 + 3195;
	exit(''.$num);
}

